package com.br.canaisfavoritos;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Test {

	private static final String TAG_A = "a";
	private static final String TAG_BODY = "body";
	private static final String TAG_DIV = "div";
	private static final String TAG_HTML = "html";
	private static final String TAG_LI = "li";
	private static final String TAG_SCRIPT = "script";
	private static final String TAG_SPAN_METADADOS = "span.metadados";
	private static final String TAG_SPAN_SMALL_DATA = "span.small_data";
	private static final String TAG_SPAN_TEXTO = "span.texto";
	private static final String TAG_SPAN_TEXTO2 = "span.texto2";
	private static final String TAG_SPAN_TIT_ORIG = "span.tit_orig";
	private static final String TAG_UL = "ul";

	private static final String ATTR_CLASS = "class";
	private static final String ATTR_CLASS_DATA_PROG = "data_prog";
	private static final String ATTR_CLASS_HOME = "home";
	private static final String ATTR_HREF = "href";
	private static final String ATTR_TITLE = "title";

	private static final String PATTERN_DATE_TIME = "dd/MMHH'h'mm";
	private static final String PATTERN_REGEX_DATE = "[^0-9/]";
	private static final String PATTERN_REGEX_NUMBERS = "[^0-9]";
	private static final String PATTERN_REGEX_REMOVE_SCRIPT = "(<.*+[^º]+str=\")|(\";.*+[^º]+>)";
	private static final String PATTERN_REGEX_TIME = "[^0-9-h]";

	private static final int DEFAULT_CHAR_BUFFER_SIZE = 512000;// 512000 = 500KB
	private static final String EMPTY = "";
	private static final String NEW_LINE = "\n";

	public static void main(String[] arg) throws IOException {
		String url = "http://meuguia.tv/programacao/canal/T2H";
		// String url = "d://a.txt";

		String htmlDaPagina = getHtmlDaPagina(url);

		Elements listaDeProgramacaoDaPaginaElements = getListaDeProgramacaoDaPaginaElements(htmlDaPagina);

		List<DadosDaProgramacaoDoCanal> teste = transformarListaDeProgramacaoDaPaginaHtmlEmObjeto(listaDeProgramacaoDaPaginaElements);
		teste.toString();
	}

	private static String getHtmlDaPagina(String url) throws IOException {
		InputStreamReader in = null;
		BufferedReader bufferedReader = null;
		StringBuilder sb = new StringBuilder();

		try {
			in = new InputStreamReader(new URL(url).openStream());
			// in = new FileReader(url);
			bufferedReader = new BufferedReader(in, DEFAULT_CHAR_BUFFER_SIZE);

			while (bufferedReader.ready()) {
				sb.append(bufferedReader.readLine()).append(NEW_LINE);
			}
		} finally {
			if (bufferedReader != null) {
				bufferedReader.close();
			}
		}

		return sb.toString();
	}

	private static Elements getListaDeProgramacaoDaPaginaElements(
			String htmlDaPagina) {
		Elements listaDeProgramacaoDaPaginaElements = Jsoup.parse(htmlDaPagina)
				.select(TAG_HTML).select(TAG_BODY).select(TAG_UL)
				.select(TAG_LI);
		listaDeProgramacaoDaPaginaElements.remove(0);
		return listaDeProgramacaoDaPaginaElements;
	}

	private static List<DadosDaProgramacaoDoCanal> transformarListaDeProgramacaoDaPaginaHtmlEmObjeto(
			Elements listaDeProgramacaoDaPaginaElements) throws IOException {
		String strDate = null;
		String strDateTime = null;
		List<DadosDaProgramacaoDoCanal> dadosDaProgramacaoDoCanalList = new ArrayList<DadosDaProgramacaoDoCanal>();

		for (Element element : listaDeProgramacaoDaPaginaElements) {
			if (ATTR_CLASS_DATA_PROG.equals(element.attr(ATTR_CLASS))) {
				strDate = element.text().replaceAll(PATTERN_REGEX_DATE, EMPTY);
				continue;
			} else if (ATTR_CLASS_HOME.equals(element.attr(ATTR_CLASS))) {
				strDateTime = strDate
						+ element.select(TAG_A).select(TAG_DIV).text()
								.toLowerCase()
								.replaceAll(PATTERN_REGEX_TIME, EMPTY);
			}

			DadosDaProgramacaoDoCanal dadosDaProgramacaoDoCanal = new DadosDaProgramacaoDoCanal();

			dadosDaProgramacaoDoCanal.setLink(element.select(TAG_A)
					.attr(ATTR_HREF).replaceAll(PATTERN_REGEX_NUMBERS, EMPTY));
			dadosDaProgramacaoDoCanal.setTitulo(element.select(TAG_A).attr(
					ATTR_TITLE));

			LocalDateTime localDateTime = LocalDateTime.parse(strDateTime,
					DateTimeFormat.forPattern(PATTERN_DATE_TIME)).withYear(
					LocalDateTime.now().getYear());
			if (localDateTime.getMonthOfYear() < LocalDateTime.now()
					.getMonthOfYear()) {
				localDateTime = localDateTime.plusYears(1);
			}
			dadosDaProgramacaoDoCanal.setDataHora(localDateTime);

			dadosDaProgramacaoDoCanal.setMetadados(element.select(TAG_A)
					.select(TAG_SPAN_METADADOS).text());

			transformarComplementoDaProgramacaoEmObjeto(dadosDaProgramacaoDoCanal);

			dadosDaProgramacaoDoCanalList.add(dadosDaProgramacaoDoCanal);
		}

		return dadosDaProgramacaoDoCanalList;
	}

	private static void transformarComplementoDaProgramacaoEmObjeto(
			DadosDaProgramacaoDoCanal dadosDaProgramacaoDoCanal)
			throws IOException {
		String htmlDaPagina = getHtmlDaPagina(dadosDaProgramacaoDoCanal
				.getLink());
		// String htmlDaPagina = getHtmlDaPagina("d://b.txt");

		Elements complementoDaProgramacao = getComplementoDaProgramacaoDaPaginaElements(htmlDaPagina);

		dadosDaProgramacaoDoCanal.setTituloOriginal(complementoDaProgramacao
				.select(TAG_SPAN_TIT_ORIG).text());
		dadosDaProgramacaoDoCanal.setSmallData(complementoDaProgramacao.select(
				TAG_SPAN_SMALL_DATA).text());
		dadosDaProgramacaoDoCanal.setTexto1(complementoDaProgramacao
				.select(TAG_SPAN_TEXTO).select(TAG_SCRIPT).toString()
				.replaceAll(PATTERN_REGEX_REMOVE_SCRIPT, EMPTY));

		Elements spanTexto2Elements = complementoDaProgramacao
				.select(TAG_SPAN_TEXTO2);
		for (Element element : spanTexto2Elements) {
			int indexOf = spanTexto2Elements.indexOf(element);
			if (indexOf == 0) {
				dadosDaProgramacaoDoCanal.setTexto2(element.text());
			}
			if (indexOf == 1) {
				dadosDaProgramacaoDoCanal.setTexto3(element.text());
			}
		}
	}

	private static Elements getComplementoDaProgramacaoDaPaginaElements(
			String htmlDaPagina) {
		Elements complementoDaProgramacaoDaPaginaElements = Jsoup
				.parse(htmlDaPagina).select(TAG_HTML).select(TAG_BODY)
				.select(TAG_UL).select(TAG_LI).select(TAG_DIV);
		return complementoDaProgramacaoDaPaginaElements;
	}
}
